﻿Imports TyranntEngine
Imports System.IO

Public Class FinishCharacterForm

    Private _token As String
    Private _char As New PlayerCharacter
    Private WithEvents _client As New TyranntOnlineCharacterService.TyranntCharacterServiceClient
    Private _currentRace As New CharacterRace
    Private _currentGuild As New CharacterGuild
    Private _currentClass As New CharacterClass

    Public Sub Init(ByVal token As String, ByVal characterXML As String)
        _token = token
        _char.LoadXML(characterXML)
    End Sub

    Private Sub FinishCharacterForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        nameTextBox.Text = _char.Name
        ' get other details from Server
        _client.GetRaceAsync(_token, _char.RaceID)
        _client.GetGuildAsync(_token, _char.GuildID)
        _client.GetClassAsync(_token, _char.ClassID)
        _client.GetCharacterSkillsAsync(_token, _char.ID)
        _client.GetCharacterItemsAsync(_token, _char.ID)
    End Sub

    Private Sub _client_GetCharacterItemsCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.GetCharacterItemsCompletedEventArgs) Handles _client.GetCharacterItemsCompleted
        If e.Result.Contains("[#]") = True Then
            If e.Result.StartsWith("[#]failed") Then
                MessageBox.Show(e.Result, "Get Items Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            Else
                MessageBox.Show(e.Result)
                Exit Sub
            End If
        End If
        Dim sr As New StringReader(e.Result)
        Dim itemsX As XElement
        itemsX = XElement.Load(sr)
        Dim count As Integer = 0
        For Each anItem In itemsX...<item>
            Dim pb As PictureBox
            Dim itemName As String = anItem.<name>.Value
            Dim iconImage As String = anItem.<icon>.Value
            Select Case count
                Case 0
                    pb = inv1PictureBox
                Case 1
                    pb = inv2PictureBox
                Case 2
                    pb = inv3PictureBox
                Case 3
                    pb = inv4PictureBox
                Case 4
                    pb = inv5PictureBox
                Case 5
                    pb = inv6PictureBox
                Case 6
                    pb = inv7PictureBox
                Case 7
                    pb = inv8PictureBox
                Case Else
                    pb = Nothing
            End Select
            If IsNothing(pb) = False Then
                pb.Image = Image.FromFile(".\media\" + iconImage)
            End If
            count += 1
        Next

    End Sub

    Private Sub _client_GetCharacterSkillsCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.GetCharacterSkillsCompletedEventArgs) Handles _client.GetCharacterSkillsCompleted
        If e.Result.Contains("[#]") = True Then
            If e.Result.StartsWith("[#]failed") Then
                MessageBox.Show(e.Result, "Get Skills Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            Else
                MessageBox.Show(e.Result)
                Exit Sub
            End If
        End If
        Dim sr As New StringReader(e.Result)
        Dim skillsX As XElement
        skillsX = XElement.Load(sr)
        skillsListBox.Items.Clear()
        For Each aSkill In skillsX...<skill>
            skillsListBox.Items.Add(aSkill.<name>.Value)
        Next

    End Sub

    Private Sub _client_GetClassCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.GetClassCompletedEventArgs) Handles _client.GetClassCompleted
        If e.Result.Contains("[#]") = True Then
            If e.Result.StartsWith("[#]failed") Then
                MessageBox.Show(e.Result, "Get Class Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            Else
                MessageBox.Show(e.Result)
                Exit Sub
            End If
        End If
        _currentClass.LoadXML(e.Result)
        classTextBox.Text = _currentClass.Name
    End Sub

    Private Sub _client_GetGuildCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.GetGuildCompletedEventArgs) Handles _client.GetGuildCompleted
        If e.Result.Contains("[#]") = True Then
            If e.Result.StartsWith("[#]failed") Then
                MessageBox.Show(e.Result, "Get Guild Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            Else
                MessageBox.Show(e.Result)
                Exit Sub
            End If
        End If
        _currentGuild.LoadXML(e.Result)
        guildTextBox.Text = _currentGuild.Name
    End Sub

    Private Sub _client_GetRaceCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.GetRaceCompletedEventArgs) Handles _client.GetRaceCompleted
        If e.Result.Contains("[#]") = True Then
            If e.Result.StartsWith("[#]failed") Then
                MessageBox.Show(e.Result, "Get Race Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            Else
                MessageBox.Show(e.Result)
                Exit Sub
            End If
        End If
        _currentRace.LoadXML(e.Result)
        raceTextBox.Text = _currentRace.Name
        racePortraitPictureBox.Image = Image.FromFile(".\media\" + _char.RacePortraitName)
    End Sub

    Private Sub RefreshForm()
        strTextBox.Text = _char.Strength.ToString
        dexTextBox.Text = _char.Dexterity.ToString
        IntTextBox.Text = _char.Intelligence.ToString
        If CheckValid() = False Then
            finishButton.Enabled = False
        Else
            finishButton.Enabled = True
        End If
    End Sub

    Private Function CheckValid() As Boolean
        If _char.Strength = 0 Or _char.Dexterity = 0 Or _char.Intelligence = 0 Then
            Return False
        Else
            Return True
        End If
    End Function

    Private Sub rollButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rollButton.Click
        rollButton.Enabled = False
        _client.GenerateStatsAsync(_token, _char.ID)
    End Sub

    Private Sub _client_GenerateStatsCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.GenerateStatsCompletedEventArgs) Handles _client.GenerateStatsCompleted
        If e.Result.Contains("[#]") = True Then
            If e.Result.StartsWith("[#]failed") Then
                MessageBox.Show(e.Result, "Generate Stats Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            Else
                MessageBox.Show(e.Result)
                Exit Sub
            End If
        End If
        rollButton.Enabled = True
        _char.LoadXML(e.Result)
        RefreshForm()
    End Sub

    Private Sub finishButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles finishButton.Click
        Me.Close()
    End Sub

    Private Sub canceledButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles canceledButton.Click
        canceledButton.Enabled = False
        finishButton.Enabled = False
        _client.DeleteCharacterAsync(_token, _char.ID)
    End Sub

    Private Sub _client_DeleteCharacterCompleted(ByVal sender As Object, ByVal e As TyranntOnlineCharacterService.DeleteCharacterCompletedEventArgs) Handles _client.DeleteCharacterCompleted
        If e.Result <> "" Then
            MessageBox.Show(e.Result, "Delete Character Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        Me.Close()
    End Sub
End Class